<?php
global $_W, $_GPC;

$config = $this->module['config'];
$op = trim(empty($_GPC['op']) ? 'list' : $_GPC['op']);

$aliyunVod = new AliyunVod($config['regionId'], $config['AccessKeyID'], $config['AccessKeySecret']);
if ($op == 'list') {
    $city = pdo_getall($this->t_city, array('status' => 1, 'weid' => $_W['uniacid']));
    $teacher = pdo_getall($this->t_teacher, array('status' => 1, 'weid' => $_W['uniacid']));
    $subject = pdo_getall($this->t_subject, array('status' => 1, 'weid' => $_W['uniacid']));
    $class = pdo_getall($this->t_class, array('status' => 1, 'weid' => $_W['uniacid']));
    $semester = pdo_getall($this->t_semester, array('status' => 1, 'weid' => $_W['uniacid']));
    $voidtype = pdo_getall($this->t_voidtype, array('status' => 1, 'weid' => $_W['uniacid']));
    $year = pdo_fetchall("SELECT `yearid` FROM " . tablename($this->t_lesson) . " WHERE weid = :weid GROUP BY yearid", array(':weid' => $_W['uniacid']));

    $contion = "l.weid = :weid";
    $params[':weid'] = $_W['uniacid'];
    $kname = trim($_GPC['kname']);


    $order = '  l.addtime DESC,`status` DESC,  `order` DESC ';
    $order_type = 1;
    $signupnum = 1;
    $price_order = 1;
    //排序值的正序与倒叙
    if (!empty($_GPC['order_type'])) {
        $order_type = $_GPC['order_type'];
        if ($order_type == 1) {
            $order = '  `order` DESC ';
        } else {
            $order = '  `order` ASC ';
        }
    }
    //报名人数的正序与倒叙
    if (!empty($_GPC['signupnum'])) {
        $signupnum = $_GPC['signupnum'];
        if ($signupnum == 1) {
            $order = '  `study_num` DESC ';
        } else {
            $order = '  `study_num` ASC ';
        }
    }
    //价格的正序与倒叙
    if (!empty($_GPC['price_order'])) {
        $price_order = $_GPC['price_order'];
        if ($price_order == 1) {
            $order = '  `pay_money` DESC ';
        } else {
            $order = '  `pay_money` ASC ';
        }
    }


    if ($kname != "") {
        $contion .= " AND l.title LIKE :kname";
        $params[':kname'] = "%" . $kname . "%";
    }
    /*$cityid = intval($_GPC['cityid']);
    if($cityid > 0){
        $contion .= " AND l.cityid = :cityid";
        $params[':cityid'] = $cityid;
    }*/
    //按日期搜索
    $date = $_GPC['date'];
    if (!empty($date)) {
        //$begintime = date('Y-m-01', strtotime($date));
        $begintime = date('Y') . '-' . $date;
        $month_start = strtotime($begintime);
        $month_end = strtotime("{$begintime} +1 month -1 day");
        $contion .= ' and l.addtime > :starttime and l.addtime < :endtime ';
        $params[':starttime'] = $month_start;
        $params[':endtime'] = $month_end;
    }
    //学科
    $subjectid = intval($_GPC['subjectid']);
    if ($subjectid > 0) {
        $contion .= " AND l.subjectid = :subjectid";
        $params[':subjectid'] = $subjectid;
    }
    //讲师
    $teacherid = intval($_GPC['teacherid']);
    if ($teacherid > 0) {
        $contion .= " AND l.teacherid = :teacherid";
        $params[':teacherid'] = $teacherid;
    }
    //年级
    $classid = intval($_GPC['classid']);
    if ($classid > 0) {
        $contion .= " AND classid LIKE '%\"" . $classid . "\"%'";
    }
    //班期
    $semesterid = intval($_GPC['semesterid']);
    if ($semesterid > 0) {
        $contion .= " AND l.semesterid = :semesterid";
        $params[':semesterid'] = $semesterid;
    }
    //视频类型
    $voidtypeid = intval($_GPC['voidtypeid']);
    if ($voidtypeid > 0) {
        $contion .= " AND l.voidtypeid = :voidtypeid";
        $params[':voidtypeid'] = $voidtypeid;
    }
    //课程课次
    $isonly = intval($_GPC['isonly']);
    if (!empty($isonly)) {
        $contion .= ' and l.is_only = :isonly ';
        $params[':isonly'] = $isonly;
    }
    //价格
    $price_status = intval($_GPC['price_type']);
    if (!empty($price_status)) {
        if ($price_status == 2) {
            //付费
            $contion .= ' and l.paytype = 4 ';

        } elseif ($price_status == 1) {
            //免费
            $contion .= ' and l.paytype = 2';
        }
    }

    /*$yearid = intval($_GPC['yearid']);
    if($yearid > 0){
        $contion .= " AND l.yearid = :yearid";
        $params[':yearid'] = $yearid;
    }*/
    $psize = 10;
    $pageindex = max(1, $_GPC['page']);
    $lessonlist = pdo_fetchall("SELECT l.id,l.title,l.image,l.order,l.status,l.classid,l.recommend,l.study_num,t.name as tname,s.name as sname,v.name as vname,l.pay_money,l.pay_num,l.moduleid,l.qrcode,l.is_only,l.is_underline FROM " . tablename($this->t_lesson) . "as l JOIN " . tablename($this->t_teacher) . " as t ON l.teacherid = t.id JOIN " . tablename($this->t_subject) . " as s ON l.subjectid = s.id  JOIN " . tablename($this->t_voidtype) . " as v ON l.voidtypeid = v.id  WHERE " . $contion . " ORDER BY " . $order . " LIMIT " . ($pageindex - 1) * $psize . "," . $psize, $params);
    $errornum = 0;

    foreach ($lessonlist as $k => $v) {
        if ($v['is_underline'] == 1) {
            $lessonlist[$k]['signupnum'] = pdo_fetchcolumn("select count(*) from " . tablename('goouc_class_lesson_underline_student') . " where lesson_id = :lessonid ", array(':lessonid' => $v['id']));
        } else {
            $lessonlist[$k]['signupnum'] = pdo_fetchcolumn("select count(*) from " . tablename($this->t_signup) . " where lessonid = :lessonid ", array(':lessonid' => $v['id']));
        }
        $lessonlist[$k]['study_num'] = pdo_fetchcolumn("select sum(num) from " . tablename('goouc_class_son') . " where lessonid = :lessonid ", array(':lessonid' => $v['id']));
    }
    $tatol = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename($this->t_lesson) . " as l WHERE " . $contion, $params);
    $page = pagination($tatol, $pageindex, $psize);
    $num = $tatol - ($pageindex - 1) * $psize;
} elseif ($op == 'post') {
    $id = intval($_GPC['id']);
    $info = pdo_fetch("SELECT l.id,l.title,l.image,l.order,l.status,l.old_money,l.fic_study_num,l.classid,l.pctype,l.is_underline,t.name as tname,l.desc,t.id as tid,s.name as sname,s.id as sid,c.name as cname,c.id as cid,v.name as vname,v.id as vid,l.pay_money,l.pay_num,l.yearid,l.classtypeid,l.is_only,l.paytype,l.xhbnumber,l.help_teacherid,l.classid,l.is_groupbuy FROM " . tablename($this->t_lesson) . "l JOIN " . tablename($this->t_teacher) . " as t ON l.teacherid = t.id JOIN " . tablename($this->t_city) . " as c ON l.cityid = c.id JOIN " . tablename($this->t_subject) . " as s ON l.subjectid = s.id  JOIN " . tablename($this->t_voidtype) . " as v ON l.voidtypeid = v.id  WHERE l.weid = :weid AND l.id = :id", array(":weid" => $_W['uniacid'], ':id' => $id));

    $info['classid'] = unserialize($info['classid']);
    $class = pdo_fetchall("SELECT * FROM " . tablename($this->t_class) . " WHERE subjectid LIKE '%\"" . $info['sid'] . "\"%'");
    $teacherlist = pdo_fetchall("SELECT `id`,`name` FROM " . tablename($this->t_teacher) . " WHERE weid = :weid and status = 1", array(':weid' => $_W['uniacid']));
    $voidtype = pdo_fetchall("SELECT `id`,`name` FROM " . tablename($this->t_voidtype) . " WHERE weid = :weid and status = 1", array(':weid' => $_W['uniacid']));
    $classtypelist = pdo_fetchall("SELECT `id`,`name` FROM " . tablename($this->t_classtype) . " WHERE weid = :weid and status = 1", array(':weid' => $_W['uniacid']));
    $semester = pdo_fetchall("SELECT `id`,`name` FROM " . tablename($this->t_semester) . " WHERE weid = :weid and status = 1", array(':weid' => $_W['uniacid']));
    //助教老师列表，去除当前主讲老师
    $help_teacherlist = pdo_fetchall("select id,name from " . tablename($this->t_teacher) . " where id <> :id and weid = :weid and status = :status order by `order` DESC", array(':id' => $info['tid'], ':weid' => $_W['uniacid'], ':status' => 1));
    /*通过老师获取到老师的相应信息*/
    if ($_W['isajax']) {
        $teacherid = intval($_GPC['teacherid']);
        $tinfo = pdo_fetch("SELECT * FROM " . tablename($this->t_teacher) . "WHERE id = :id AND weid = :weid", array(':id' => $teacherid, ':weid' => $_W['uniacid']));
        $city = pdo_fetch("SELECT `id`,`name` FROM " . tablename($this->t_city) . " WHERE id = :id AND weid = :weid", array(':id' => $tinfo['cityid'], ':weid' => $_W['uniacid']));
        $subject = pdo_fetch("SELECT `id`,`name` FROM " . tablename($this->t_subject) . " WHERE id = :id AND weid = :weid", array(':id' => $tinfo['subjectid'], ':weid' => $_W['uniacid']));
        $class = pdo_fetchall("SELECT * FROM " . tablename($this->t_class) . " WHERE weid = :weid AND subjectid LIKE '%\"" . $subject['id'] . "\"%'", array(':weid' => $_W['uniacid']));
        $sql = 'select id,name from ' . tablename($this->t_teacher) . 'where id <> :id and weid = :weid and status = :status order by `order` DESC';
        $params = [':id' => $teacherid, ':weid' => $_W['uniacid'], ':status' => 1];
        $help_teacherlist = pdo_fetchall($sql, $params);
        $arr = array('city' => $city, 'subject' => $subject, 'class' => $class, 'help_teacher' => $help_teacherlist);
        echo json_encode($arr);
        exit;
    }
    //做提交
    if (checksubmit('submit')) {
        $id = intval($_GPC['id']);
        $data['weid'] = $_W['uniacid'];
        $data['title'] = trim($_GPC['title']);
        $data['teacherid'] = $_GPC['teacherid'];
        $data['image'] = $_GPC['image'];
        $data['voidtypeid'] = $_GPC['voidtypeid'];
        $data['semesterid'] = $_GPC['semesterid'];
        $data['cityid'] = $_GPC['cityid'];
        $data['yearid'] = $_GPC['yearid'];
        $data['classid'] = serialize($_GPC['classid']);
        $data['subjectid'] = $_GPC['subjectid'];
        $data['order'] = empty($_GPC['order']) ? 0 : $_GPC['order'];
        $data['status'] = empty($_GPC['status']) ? 0 : $_GPC['status'];
        $data['is_only'] = empty($_GPC['is_only']) ? 2 : $_GPC['is_only'];
        $data['classtypeid'] = $_GPC['classtypeid'];
        $data['paytype'] = empty($_GPC['paytype']) ? 2 : $_GPC['paytype'];
        $data['help_teacherid'] = intval($_GPC['help_teacherid']);
        $data['pctype'] = intval($_GPC['pctype']);
        $data['is_underline'] = intval($_GPC['is_underline']);
        if ($data['paytype'] == 3) {
            $data['xhbnumber'] = empty($_GPC['xhbnumber']) ? 1 : $_GPC['xhbnumber'];
        } elseif ($data['paytype'] == 4) {
            $data['is_groupbuy'] = empty($_GPC['is_groupbuy']) ? 2 : $_GPC['is_groupbuy'];
            $data['pay_money'] = empty($_GPC['pay_money']) ? '0.00' : $_GPC['pay_money'];
            if ($data['is_groupbuy'] == 1) {
                $data['old_money'] = empty($_GPC['old_money']) ? '0.00' : $_GPC['old_money'];
            }
        }
        $data['fic_study_num'] = empty($_GPC['fic_study_num']) ? 0 : $_GPC['fic_study_num'];
        $data['desc'] = $_GPC['desc'];
        //var_dump($data);die();
        if ($this->userid == $_W['uid']) {
            message($this->msg);
        }
        /*if(empty($data['teacherid']) || empty($data['subjectid']) || empty($data['cityid']) || empty($data['voidtypeid']) || empty($data['semesterid']) || empty($data['image']) || empty($data['title']) || empty($data['desc'])){
            itoast('讲师、学科、城市、视频类型、班期、分享封面图片选择不能为空');
        }*/
        $data['pay_money'] = empty($_GPC['pay_money']) ? '0.00' : round($_GPC['pay_money'], 2);
        if (empty($id)) {
            $data['addtime'] = time();
            $res = pdo_insert($this->t_lesson, $data);
            if ($res) {
                $arr = array(
                    'weid' => $_W['uniacid'],
                    'admin_uid' => $_W['uid'],
                    'admin_username' => $_W['username'],
                    'log_type' => 1,
                    'function' => '课程管理->新增课程',
                    'content' => '新增课程:' . trim($_GPC['title'])
                );
                $this->addlog($arr);
                message('操作成功', $this->createWebUrl('lesson'), 'succss');
            } else {
                message('操作失败', $this->createWebUrl('lesson'), 'error');
            }
        } else {
            $res = pdo_update($this->t_lesson, $data, array('id' => $id));
            if ($res) {
                $arr = array(
                    'weid' => $_W['uniacid'],
                    'admin_uid' => $_W['uid'],
                    'admin_username' => $_W['username'],
                    'log_type' => 3,
                    'function' => '课程管理->修改课程',
                    'content' => '修改课程:' . trim($_GPC['title'])
                );
                $this->addlog($arr);
                message('操作成功', $this->createWebUrl('lesson'), 'succss');
            } else {
                message('操作失败', $this->createWebUrl('lesson'), 'error');
            }
        }
    }
} elseif ($op == 'delete') {
    if ($this->userid == $_W['uid']) {
        message($this->msg);
    }
    $id = intval($_GPC['id']);
    $sonlist = pdo_fetch("SELECT * FROM " . tablename($this->t_son) . ' WHERE lessonid = :lessonid AND weid = :weid', array(':lessonid' => $id, ":weid" => $_W['uniacid']));

    if ($sonlist > 0) {
        itoast('课程下有章节信息，不能删除课程', 'error');
    }
    $title = pdo_get($this->t_lesson, array('id' => $id), array('title'));
    $res = pdo_delete($this->t_lesson, array('id' => $id));
    if ($res) {
        $arr = array(
            'weid' => $_W['uniacid'],
            'admin_uid' => $_W['uid'],
            'admin_username' => $_W['username'],
            'log_type' => 2,
            'function' => '课程管理->删除课程',
            'content' => '删除课程:' . $title['title']
        );
        $this->addlog($arr);
        message('操作成功', $this->createWebUrl('lesson'), 'succss');
    } else {
        message('操作失败', $this->createWebUrl('lesson'), 'error');
    }
} elseif ($op == 'cstatus') {
    if ($this->userid == $_W['uid']) {
        $message = array('success' => 0, 'mess' => $this->msg);
        echo json_encode($message);
        exit;
    }
    $id = $_GPC['id'];
    $sta = $_GPC['sta'];
    $data['status'] = $sta;

    $info = pdo_get($this->t_lesson, array('id' => $id, 'weid' => $_W['uniacid']));
    if ($sta == $info['status']) {
        $message = array('success' => 3, 'mess' => '更改失败，请刷新页面后再更改');
    } else {
        $res = pdo_update($this->t_lesson, $data, array('id' => $id, 'weid' => $_W['uniacid']));
        if ($res) {
            $message = array('success' => 1, 'mess' => '更改成功');
        } else {
            $message = array('success' => 0, 'mess' => '更改失败');
        }
    }
    echo json_encode($message);
    exit;
} elseif ($op == 'module') {
    //获取一下模块的列表
    if ($_W['isajax']) {
        $lid = $_GPC['lid'];
        $m_list = pdo_getall('goouc_class_module', array('weid' => $_W['uniacid'], 'status' => 1), array('id', 'name'));
        return include $this->template('web/choose');
        die;
    }
} elseif ($op == 'selectm') {
    //一个课程对应一个模块
    if ($_W['isajax']) {
        $mid = $_GPC['mid'];
        $lid = $_GPC['lid'];
        $data['moduleid'] = $mid;
        if ($this->userid == $_W['uid']) {
            $message = array('success' => 0, 'mess' => $this->msg);
            echo json_encode($message);
            exit;
        }
        //查询一下当前课程对应的模块值为0  则添加 否则不添加
        $res = pdo_update($this->t_lesson, $data, array('id' => $lid, 'weid' => $_W['uniacid']));
        if ($res) {
            $message = array('success' => 1, 'mess' => '添加成功');
        } else {
            $message = array('success' => 0, 'mess' => '添加失败');
        }
        echo json_encode($message);
        exit;
    }
} elseif ($op == 'recommend') {
    if ($_W['isajax']) {
        $id = $_GPC['id'];
        $sta = $_GPC['sta'];
        $data['recommend'] = $sta;
        $info = pdo_get($this->t_lesson, array('id' => $id, 'weid' => $_W['uniacid']));
        if ($this->userid == $_W['uid']) {
            $message = array('success' => 0, 'mess' => $this->msg);
            echo json_encode($message);
            exit;
        }
        if ($sta == $info['recommend']) {
            $message = array('success' => 3, 'mess' => '更改失败，请刷新页面后再更改');
        } else {
            $res = pdo_update($this->t_lesson, $data, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($res) {
                $message = array('success' => 1, 'mess' => '更改成功');
            } else {
                $message = array('success' => 0, 'mess' => '更改失败');
            }
        }
        echo json_encode($message);
        exit;
    }
} elseif ($op == 'qrcode') {
    if ($_W['isajax']) {
        if ($this->userid == $_W['uid']) {
            $message = array('success' => 0, 'mess' => $this->msg);
            echo json_encode($message);
            exit;
        }
        $lessonid = $_GPC['lessonid'];
        //静态
        $appid = $_W['account']['key'];
        $secret = $_W['account']['secret'];
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
        $access_token = json_decode($this->curl_get($url), true)['access_token'];
        $data = array();
        $data['page'] = 'pages/home/home';//路径
        $data['scene'] = 'p=SD&l=' . $lessonid;
        // $data['width'] = 430;
        $data = json_encode($data);
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
        $da = $this->get_http_array($url, $data);
        // 生成一个图片
        $path = MODULE_ROOT . '/Qrcode/lesson/';
        if (!is_dir($path)) {
            mkdir($path);
        }
        $filename = $lessonid . '_课程.jpg';
        file_put_contents($path . $filename, $da);

        $res = pdo_update($this->t_lesson, array('qrcode' => MODULE_URL . 'Qrcode/lesson/' . $filename), array('weid' => $_W['uniacid'], 'id' => $lessonid));
        $qrcode = MODULE_URL . 'Qrcode/lesson/' . $filename;
        if ($res) {
            $message = array('success' => 1, 'mess' => $qrcode);
        } else {
            $message = array('success' => 0);
        }
        echo json_encode($message);
        exit;
    }
} elseif ($op == 'download') {
    if ($this->userid == $_W['uid']) {
        message($this->msg);
    }
    $lessonid = $_GPC['lessonid'];
    $lessoninfo = pdo_get($this->t_lesson, array('id' => $lessonid, 'weid' => $_W['uniacid']));
    $path = MODULE_ROOT . '/Qrcode/lesson/';
    $filename = $lessonid . '_课程.jpg';
    header('Content-Type:text/html;charset=utf-8');
    header("Content-Type: application/jpg");
    header("Content-Transfer-Encoding: Binary");
    header("Content-Length: " . filesize($path . $filename));
    header("Content-Disposition: attachment; filename=\"$filename\"");
    readfile($path . $filename);
} elseif ($op == 'downloadlesson') {
    $name = '课程列表';
    $dcontion = " l.weid = :weid ";
    $dparams = array(':weid' => $_W['uniacid']);

    $kname = trim($_GPC['kname']);
    if ($kname != "") {
        $dcontion .= " AND l.title LIKE :kname";
        $dparams[':kname'] = "%" . $kname . "%";
    }
    /*$cityid = intval($_GPC['cityid']);
    if($cityid > 0){
        $contion .= " AND l.cityid = :cityid";
        $params[':cityid'] = $cityid;
    }*/
    $date = $_GPC['date'];
    if (!empty($date)) {
        $begintime = date('Y-m-01', strtotime($date));
        $month_start = strtotime($begintime);
        $month_end = strtotime("$begintime +1 month -1 day");
        $dcontion .= ' and l.addtime > :starttime and l.addtime < :endtime ';
        $dparams[':starttime'] = $month_start;
        $dparams[':endtime'] = $month_end;
    }
    $subjectid = intval($_GPC['subjectid']);
    if ($subjectid > 0) {
        $dcontion .= " AND l.subjectid = :subjectid";
        $dparams[':subjectid'] = $subjectid;
    }
    $teacherid = intval($_GPC['teacherid']);
    if ($teacherid > 0) {
        $dcontion .= " AND l.teacherid = :teacherid";
        $dparams[':teacherid'] = $teacherid;
    }
    $classid = intval($_GPC['classid']);
    if ($classid > 0) {
        $dcontion .= " AND classid LIKE '%\"" . $classid . "\"%'";
    }
    $semesterid = intval($_GPC['semesterid']);
    if ($semesterid > 0) {
        $dcontion .= " AND l.semesterid = :semesterid";
        $dparams[':semesterid'] = $semesterid;
    }
    $voidtypeid = intval($_GPC['voidtypeid']);
    if ($voidtypeid > 0) {
        $dcontion .= " AND l.voidtypeid = :voidtypeid";
        $dparams[':voidtypeid'] = $voidtypeid;
    }
    /* $yearid = intval($_GPC['yearid']);
     if($yearid > 0){
         $dcontion .= " AND l.yearid = :yearid";
         $dparams[':yearid'] = $yearid;
     }*/
    $lessonlist = pdo_fetchall("select l.id,l.title,l.pay_money,l.old_money,t.name,l.study_num,l.classid,l.addtime from " . tablename($this->t_lesson) . " l JOIN " . tablename($this->t_teacher) . " t ON l.teacherid = t.id where " . $dcontion . " order by l.status DESC,l.order DESC,l.id DESC ", $dparams);
    $class = [];
    foreach ($lessonlist as $k => $v) {
        $class = unserialize($v['classid']);
        $class_str = implode(',', $class);
        $class_list = pdo_fetchall("select name from " . tablename($this->t_class) . " where id in (" . $class_str . ") ");
        $lessonlist[$k]['class'] = implode(';', array_column($class_list, 'name'));
        $lessonlist[$k]['sginupnum'] = pdo_fetchcolumn("select count(*) from " . tablename($this->t_signup) . " where lessonid = :lessonid ", array(':lessonid' => $v['id']));
        $lessonlist[$k]['study_num'] = pdo_fetchcolumn("select sum(num) from " . tablename('goouc_class_son') . " where lessonid = :lessonid ", array(':lessonid' => $v['id']));
    }
    //制作表格
    $objPHPExcel = new PHPExcel;
    $objPHPExcel->getProperties()->setCreator("zxf")->setLastModifiedBy("zxf")->setTitle('数据EXCEL导出')->setSubject('数据EXCEL导出')->setDescription('导出数据')->setKeywords("excel")->setCategory("result file");
    $objPHPExcel->setActiveSheetIndex(0);

    $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);//行高

    $objPHPExcel->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('1')->getFont()->setSize(14);     //字体大小
    $objPHPExcel->getActiveSheet()->getStyle('1')->getFont()->setBold(true);      //第一行是否加粗

    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(45);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(45);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);//列宽

    $objPHPExcel->getActiveSheet()->setCellValue('A1', '课程名称');
    $objPHPExcel->getActiveSheet()->setCellValue('B1', '讲师');
    $objPHPExcel->getActiveSheet()->setCellValue('C1', '适用年级');
    $objPHPExcel->getActiveSheet()->setCellValue('D1', '听课人次');
    $objPHPExcel->getActiveSheet()->setCellValue('E1', '价格');
    $objPHPExcel->getActiveSheet()->setCellValue('F1', '报名人数');
    $objPHPExcel->getActiveSheet()->setCellValue('G1', '创建时间');
    $key = 2;
    foreach ($lessonlist as $k => $v) {
        $objPHPExcel->getActiveSheet()->setCellValue('A' . $key, $v['title']);
        $objPHPExcel->getActiveSheet()->setCellValue('B' . $key, $v['name']);
        $objPHPExcel->getActiveSheet()->setCellValue('C' . $key, $v['class']);
        $objPHPExcel->getActiveSheet()->setCellValue('D' . $key, $v['study_num']);
        $objPHPExcel->getActiveSheet()->setCellValue('E' . $key, $v['pay_money']);
        $objPHPExcel->getActiveSheet()->setCellValue('F' . $key, $v['study_num']);
        $objPHPExcel->getActiveSheet()->setCellValue('G' . $key, date('Y/m/d', $v['addtime']));
        $key++;
    }
    ob_end_clean();
    header('Content-Type: application/vnd.ms-excel'); //文件类型
    header('Content-Disposition: attachment;filename="' . $name . '.xls"'); //文件名
    header('Cache-Control: max-age=0');
    header('Content-Type: text/html; charset=utf-8'); //编码
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel 2003
    $objWriter->save('php://output');
    exit;
} elseif ($op == 'lessondown') {
    //导出该课程用户报名情况
    $lessonid = $_GPC['lessonid'];
    $lessoninfo = pdo_get($this->t_lesson, ['id' => $lessonid]);
    $list = pdo_fetchall("select u.nickname,u.name,u.phone,us.uid,us.lessonid,us.type,us.createtime from " . tablename($this->t_signup) . " us JOIN " . tablename($this->t_user) . " u ON u.id = us.uid where us.lessonid = :lessonid ", [':lessonid' => $lessonid]);

    foreach ($list as $k => $v) {
        $list[$k]['nickname'] = $this->removeEmoji(json_decode($list[$k]['nickname']));
        //查询user_study_speed 获取用户对课程的学习时长
        $time = pdo_fetchcolumn("select SUM(speed) from " . tablename($this->t_user_study_speed) . " where uid = :uid and lessonid = :lessonid and weid = :weid ", [':uid' => $v['uid'], 'lessonid' => $lessonid, ':weid' => $_W['uniacid']]);
        $list[$k]['looktime'] = ceil($time / 60);
        switch ($v['type']) {
            case 2 : //免费报名
                $list[$k]['mkordertime'] = date('Y/m/d H:i:s', $v['createtime']);
                $list[$k]['paytime'] = date('Y/m/d H:i:s', $v['createtime']);
                break;
            case 3 ://兑换币
                $list[$k]['mkordertime'] = date('Y/m/d H:i:s', $v['createtime']);
                $list[$k]['paytime'] = date('Y/m/d H:i:s', $v['createtime']);
                break;
            case 4 ://原价购买
                $order_info = pdo_fetch("select `createtime`,`paytime` from " . tablename($this->t_order) . " where userid =:userid and dataid = :dataid and weid = :weid and order_status = :order_status and refund_status = NULL", [':userid' => $v['uid'], ':dataid' => $lessonid, ':weid' => $_W['uniacid'], ':order_status' => 1]);
                $list[$k]['mkordertime'] = date('Y/m/d H:i:s', $order_info['createtime']);
                $list[$k]['paytime'] = date('Y/m/d H:i:s', $order_info['paytime']);
                break;
            case 5 ://团购
                $group_info = pdo_fetch("select o.createtime,o.paytime from " . tablename($this->t_user_group_info) . " ugi JOIN " . tablename($this->t_order) . " o ON ugi.out_trade_no = o.out_trade_no where ugi.uid = :uid and ugi.lessonid = :lessonid and ugi.refund_status = :refund_status", [':uid' => $v['uid'], ':lessonid' => $lessonid, ':refund_status' => 2]);
                $list[$k]['mkordertime'] = date('Y/m/d H:i:s', $group_info['createtime']);
                $list[$k]['paytime'] = date('Y/m/d H:i:s', $group_info['paytime']);
                break;
        }
    }
    unset($v);
    //制作表格
    $name = $lessoninfo['title'];
    $objPHPExcel = new PHPExcel;
    $objPHPExcel->getProperties()->setCreator("zxf")->setLastModifiedBy("zxf")->setTitle('数据EXCEL导出')->setSubject('数据EXCEL导出')->setDescription('导出数据')->setKeywords("excel")->setCategory("result file");
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->getStyle('A')->getNumberFormat()
        ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
    $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);//行高

    $objPHPExcel->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle('1')->getFont()->setSize(14);     //字体大小
    $objPHPExcel->getActiveSheet()->getStyle('1')->getFont()->setBold(true);      //第一行是否加粗

    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);//列宽
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);//列宽

    $objPHPExcel->getActiveSheet()->setCellValue('A1', '购买者');
    $objPHPExcel->getActiveSheet()->setCellValue('B1', '手机号');
    $objPHPExcel->getActiveSheet()->setCellValue('C1', '下单时间');
    $objPHPExcel->getActiveSheet()->setCellValue('D1', '支付时间');
    $objPHPExcel->getActiveSheet()->setCellValue('E1', '学习时长');
    $key = 2;
    foreach ($list as $k => $v) {
        $objPHPExcel->getActiveSheet()->setCellValue('A' . $key, $v['nickname']);
        $objPHPExcel->getActiveSheet()->setCellValue('B' . $key, $v['phone']);
        $objPHPExcel->getActiveSheet()->setCellValue('C' . $key, $v['mkordertime']);
        $objPHPExcel->getActiveSheet()->setCellValue('D' . $key, $v['paytime']);
        $objPHPExcel->getActiveSheet()->setCellValue('E' . $key, $v['looktime'] . '分钟');
        $key++;
    }
    ob_end_clean();
    header('Content-Type: application/vnd.ms-excel'); //文件类型
    header('Content-Disposition: attachment;filename="' . $name . '.xls"'); //文件名
    header('Cache-Control: max-age=0');
    header('Content-Type: text/html; charset=utf-8'); //编码
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel 2003
    $objWriter->save('php://output');
    exit;
} elseif ($op == 'underline_list') {

    $lesson_id = intval($_GPC['lesson_id']);
    $lessontitle = pdo_fetchcolumn("select title from " . tablename("goouc_class_lesson") . " where weid = :weid AND id = :lessonid ", array(':weid' => $_W['uniacid'], ':lessonid' => $lesson_id));

    $where = ' FROM ' . tablename('goouc_class_lesson_underline_student') . ' WHERE `weid`=:weid AND lesson_id = :lesson_id ';
    $params = array();
    $params[':weid'] = $_W['uniacid'];
    $params[':lesson_id'] = $lesson_id;

    $sql = 'SELECT COUNT(id) ' . $where;
    $total = pdo_fetchcolumn($sql, $params);

    if ($total > 0) {
        $pindex = max(1, intval($_GPC['page']));
        $psize = 15;

        $sql = 'SELECT * ' . $where . ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
        $list = pdo_fetchall($sql, $params);
        $pager = pagination($total, $pindex, $psize);
    }
} elseif ($op == 'editunderline') {

    $id = intval($_GPC['id']);
    $lesson_id = intval($_GPC['lesson_id']);

    $sql = "SELECT * FROM " . tablename('goouc_class_lesson_underline_student') . " WHERE `weid`=:weid AND id = :id AND lesson_id = :lesson_id ";
    $params = array();
    $params[':weid'] = $_W['uniacid'];
    $params[':id'] = $id;
    $params[':lesson_id'] = $lesson_id;
    $info = pdo_fetch($sql, $params);

    if (checksubmit('submit')) {

        $lesson_id = intval($_GPC['lesson_id']);
        if (empty(trim($_GPC['phone']))) {
            message('抱歉，请输入手机号！');
        }

        $data = array(
            'username' => trim($_GPC['username']),
            'phone' => trim($_GPC['phone']),
            'lesson_id' => $lesson_id,
        );

        $phone = pdo_getcolumn('goouc_class_lesson_underline_student', array('weid' => $_W['uniacid'], 'lesson_id' => intval($lesson_id), 'phone' => trim($_GPC['phone'])), 'id');

        if (!empty($id)) {
            if (empty($phone)) {
                pdo_update('goouc_class_lesson_underline_student', $data, array('id' => $id));
            }
        } else {
            $data['createtime'] = TIMESTAMP;
            $data['weid'] = $_W['uniacid'];
            if (empty($phone)) {
                pdo_insert('goouc_class_lesson_underline_student', $data);
            }
            $id = pdo_insertid();
        }
        message('操作成功！', $this->createWebUrl('lesson', array('op' => 'underline_list', 'lesson_id' => $lesson_id)), 'success');
    }
} elseif ($op == 'sort') {
    if ($this->userid == $_W['uid']) {
        $message = array('success' => 0, 'mess' => $this->msg);
        echo json_encode($message);
        exit;
    }
    $lid = $_GPC['lid'];
    $data['order'] = $_GPC['order'];

    $res = pdo_update($this->t_lesson, $data, array('id' => $lid, 'weid' => $_W['uniacid']));
    if ($res) {
        $message = array('success' => 1, 'mess' => '修改成功');
    } else {
        $message = array('success' => 0, 'mess' => '修改失败');
    }
} elseif ($op == 'delunderline') {

    $id = intval($_GPC['id']);
    $lesson_id = intval($_GPC['lesson_id']);

    $res = pdo_delete('goouc_class_lesson_underline_student', array('id' => $id, 'lesson_id' => $lesson_id));
    if ($res) {
        message('操作成功', $this->createWebUrl('lesson', array('op' => 'underline_list', 'lesson_id' => $lesson_id)), 'succss');
    } else {
        message('操作失败', $this->createWebUrl('lesson', array('op' => 'underline_list', 'lesson_id' => $lesson_id)), 'error');
    }

} elseif ($op == 'uploadunderline') {

    $lesson_id = intval($_GPC['lesson_id']);
    $file = $_FILES['biaoge'];

    if ($file['name'] && $file['error'] == 0) {
        $type = @end(explode('.', $file['name']));
        $type = strtolower($type);
    }
    if (!in_array($type, array('xls', 'xlsx'))) {
        echo json_encode(array('mess' => '文件类型错误！', 'code' => 201));
        exit;
    }

    date_default_timezone_set('PRC');

    if ($type == 'xls') {
        $inputFileType = 'Excel5';//这个是读 xls的
    } else {
        $inputFileType = 'Excel2007';//这个是计xlsx的
    }
    // 读取excel文件
    try {
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($file['tmp_name']);
        $objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数
        $highestRow = $objWorksheet->getHighestRow();//取得总列数
    } catch (Exception $e) {
        echo json_encode(array('mess' => '读取excel文件失败', 'code' => 201));
        exit;
    }

    $data = array();

    for ($row = 2; $row <= $highestRow; $row++) {
        $username = trim($objWorksheet->getCellByColumnAndRow(0, $row)->getValue());
        $phone = trim($objWorksheet->getCellByColumnAndRow(1, $row)->getValue());
        $rule = "/^[1][3,4,5,6,7,8,9][0-9]{9}$/";
        $res = preg_match($rule, $phone);
        if ($res) {
            $has = pdo_getcolumn('goouc_class_lesson_underline_student', array('weid' => $_W['uniacid'], 'phone' => trim($phone), 'lesson_id' => intval($lesson_id)), 'id');
            if (empty($has)) {
                $data[] = array(
                    'weid' => $_W['uniacid'],
                    'username' => $username,
                    'phone' => $phone,
                    'lesson_id' => $lesson_id,
                );
            }
        }
    }
    foreach ($data as $k => $v) {
        pdo_insert('goouc_class_lesson_underline_student', $v);
    }
    echo json_encode(array('mess' => '操作成功！', 'code' => 200));
    exit;
}
include $this->template('web/lesson');
?>